package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.ReportLqYcjldnfxjlbBaDetail;
import com.ruoyi.report_lq.domain.vo.ReportLqYcjldnfxjlbBaDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportRhlqSssylBaDetailBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqSssylBaDetailVo;
import com.ruoyi.report_lq.domain.ReportRhlqSssylBaDetail;
import com.ruoyi.report_lq.mapper.ReportRhlqSssylBaDetailMapper;
import com.ruoyi.report_lq.service.IReportRhlqSssylBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 乳化沥青筛上剩余量Service业务层处理
 *
 * @author zyb
 * @date 2024-07-02
 */
@RequiredArgsConstructor
@Service
public class ReportRhlqSssylBaDetailServiceImpl implements IReportRhlqSssylBaDetailService {

    private final ReportRhlqSssylBaDetailMapper baseMapper;

    /**
     * 查询乳化沥青筛上剩余量
     */
    @Override
    public ReportRhlqSssylBaDetailVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     *
     * @param infoId
     * @return 剩余量集合
     * @method 根据info-id 查询剩余量子表的list集合数据
     */
    public List<ReportRhlqSssylBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<ReportRhlqSssylBaDetail> query = new QueryWrapper<>();
        query.eq("common_id", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询乳化沥青筛上剩余量列表
     */
    @Override
    public TableDataInfo<ReportRhlqSssylBaDetailVo> queryPageList(ReportRhlqSssylBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportRhlqSssylBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportRhlqSssylBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询乳化沥青筛上剩余量列表
     */
    @Override
    public List<ReportRhlqSssylBaDetailVo> queryList(ReportRhlqSssylBaDetailBo bo) {
        LambdaQueryWrapper<ReportRhlqSssylBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportRhlqSssylBaDetail> buildQueryWrapper(ReportRhlqSssylBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportRhlqSssylBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommonId() != null, ReportRhlqSssylBaDetail::getCommonId, bo.getCommonId());
        lqw.eq(StringUtils.isNotBlank(bo.getShiybh()), ReportRhlqSssylBaDetail::getShiybh, bo.getShiybh());
        lqw.eq(bo.getRuhlqsyzl() != null, ReportRhlqSssylBaDetail::getRuhlqsyzl, bo.getRuhlqsyzl());
        lqw.eq(bo.getShailjjspzl() != null, ReportRhlqSssylBaDetail::getShailjjspzl, bo.getShailjjspzl());
        lqw.eq(bo.getLvsjspysssywhjzl() != null, ReportRhlqSssylBaDetail::getLvsjspysssywhjzl, bo.getLvsjspysssywhjzl());
        lqw.eq(bo.getShaissywzl() != null, ReportRhlqSssylBaDetail::getShaissywzl, bo.getShaissywzl());
        lqw.eq(bo.getShaissywhl() != null, ReportRhlqSssylBaDetail::getShaissywhl, bo.getShaissywhl());
        lqw.eq(bo.getShaissywhlpjz() != null, ReportRhlqSssylBaDetail::getShaissywhlpjz, bo.getShaissywhlpjz());
        return lqw;
    }

    /**
     * 新增乳化沥青筛上剩余量
     */
    @Override
    public Boolean insertByBo(ReportRhlqSssylBaDetailBo bo) {
        ReportRhlqSssylBaDetail add = BeanUtil.toBean(bo, ReportRhlqSssylBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改乳化沥青筛上剩余量
     */
    @Override
    public Boolean updateByBo(ReportRhlqSssylBaDetailBo bo) {
        ReportRhlqSssylBaDetail update = BeanUtil.toBean(bo, ReportRhlqSssylBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 乳化沥青筛上剩余量、微粒离子电荷试验检测记录表  -- 子表操作
     */
    public Boolean insertOrUpdateBySylBo(List<ReportRhlqSssylBaDetailVo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            ReportRhlqSssylBaDetail detail = new ReportRhlqSssylBaDetail();
            detail.setExcelId(detailVo.getExcelId());
            detail.setCommonId(pId);
            detail.setShiybh(detailVo.getShiybh());
            detail.setRuhlqsyzl(detailVo.getRuhlqsyzl());
            detail.setShaissywhl(detailVo.getShaissywhl());
            detail.setShailjjspzl(detailVo.getShailjjspzl());
            detail.setLvsjspysssywhjzl(detailVo.getLvsjspysssywhjzl());
            detail.setShaissywzl(detailVo.getShaissywzl());
            detail.setShaissywhl(detailVo.getShaissywhl());
            detail.setShaissywhlpjz(detailVo.getShaissywhlpjz());
            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }
    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportRhlqSssylBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除乳化沥青筛上剩余量
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
